<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */
use Think\Model\RelationModel;

/**
 * 应用需要执行的方法 逻辑定义  
 */
class application extends RelationModel {

    public $tableName = 'application'; // 应用表            
    public $app_path;
    public $controllerName = 'AppDistribut'; //控制器名

    public function __construct() {
        parent::__construct();
        $this->app_path = dirname($_SERVER['SCRIPT_FILENAME']) . '/'; // 当前项目路径   
    }

    /**
     * 安装          
     */
    function install() {
        $xiarikui_version = file_get_contents($this->app_path . 'Application/Admin/Conf/version.txt'); // XiaRiKui 版本
        $config = include $this->app_path . 'Public/application/' . strtolower($this->controllerName) . '/config.php'; // 当前应用适合哪些版本
        $config['version'] = explode(',', $config['version']);
        if (!in_array($xiarikui_version, $config['version'])) {
            $info['status'] = 0;
            $info['msg'] = '版本不兼容';
            return $info;
        }
        $this->isEdit();
        // 复制文件            
        recurse_copy($this->app_path . 'Public/application/' . strtolower($this->controllerName) . '/www/', $this->app_path);
//        $info['status'] = 0;
//        $info['msg'] = '安装成功,请刷新页面!';
//        return $info;
    }

    /**
     *  卸载应用
     */
    function uninstall() {
        $this->isEdit();
        delFile($this->app_path . 'Application/Admin/Controller/' . $this->controllerName . 'Controller.class.php');
        delFile($this->app_path . 'Application/Admin/View/' . $this->controllerName);
        rmdir($this->app_path . 'Application/Admin/View/' . $this->controllerName);
    }

    /**
     * 安装 sql 语句
     */
    function install_sql() {
        $this->isEdit();
        $sql = file_get_contents($this->app_path . 'Public/application/' . strtolower($this->controllerName) . '/install.sql');
        return $sql;
    }

    /**
     * 卸载 sql 语句
     */
    function uninstall_sql() {
        $this->isEdit();
        $sql = file_get_contents($this->app_path . 'Public/application/' . strtolower($this->controllerName) . '/uninstall.sql');
        return $sql;
    }

    /**
     * 判断文件是否修改
     */
    function isEdit() {
        $controllerfile = $this->app_path . '/Application/Admin/Controller/' . $this->controllerName . 'Controller.class.php';
        if (md5_file($controllerfile) != '' && md5_file($controllerfile) != '296249ee89bd78c542fa8151c6831517') {
            $info['status'] = 0;
            $info['msg'] = 'ApplicationController.class.php 文件被修改过,不能安装';
//             return $info;
            exit();
        }
    }

}
